<?php
/**
 * +----------------------------------------------------------------------
 * | summer framework
 * +----------------------------------------------------------------------
 * | Copyright (c) 2020 https://github.com/suframe/think-admin All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: summer <806115620@qq.com>  2020/4/18 14:30
 * +----------------------------------------------------------------------
 */
declare (strict_types = 1);

namespace app\mall\model\traits;

use app\mall\model\MallStoreClerk;
use think\model\relation\BelongsTo;
use think\model\relation\HasOne;

/**
 * 获取店员
 * trait ClerkTrait
 * @package app\mall\model\traits
 * @mixin \think\Model
 * @property integer store_clerk_id
 * @property MallStoreClerk clerk
 * @property array clerk_info
 */
trait ClerkTrait
{

    /**
     * 获取门店
     * @return BelongsTo
     */
    public function clerk(): HasOne
    {
        return $this->hasOne(MallStoreClerk::class, 'id', 'store_clerk_id')->cache('modelHasOneClerk' .$this->store_clerk_id, 60);
    }

    public function getClerkInfoAttr()
    {
        return $this->clerk ? $this->clerk->toArray() : [];
    }

    /**
     * 获取店员名称
     * @return string
     */
    public function getClerkNameAttr()
    {
        return $this->getClerkInfo('name');
    }

    private function getClerkInfo($key = null)
    {
        $model = $this->clerk();
        if(!$model){
            return '';
        }
        return $key === null ? $model : $model->value($key . '');
    }

}